/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.eleit;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

/**
 *
 * @author menderleit
 */
public class FullScreen {
    
    private boolean runFlag;
    private JFrame frame;
    private Window win;
    private int width;
    private int width2;
    private int height;
    private int height2;
    private int depth;
    private static BufferStrategy bs;
    private static GraphicsDevice gd;
    private static DisplayMode[] dm;
    private static GraphicsConfiguration gc;
    private static GraphicsEnvironment env;
    private static int dMode = -1;
    
    public FullScreen() {
        width = 800;
        height = 800;
        depth = 32;
        init();
    }
    
    public FullScreen(int width, int height) {
        this.width = width;
        this.height = height;
        this.depth = 32;
        init();
    }
    
    public FullScreen(int width, int height, int depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
        init();
    }
    
    private void init() {
        runFlag = true;
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setLocation(0, 0);
        frame.setResizable(false);
        frame.setBackground(Color.black);
        frame.setIgnoreRepaint(true);
        frame.setVisible(true);
    
        win = new Window(frame);
        win.setFocusable(true);
        win.setAutoRequestFocus(true);
        
        win.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent ke) {
                int key = ke.getKeyCode();
                switch(key) {
                    case KeyEvent.VK_ESCAPE:
                        runFlag = false;
                        quit();
                        break;
                }
            }
        });

        env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gd = env.getDefaultScreenDevice();
        dm = gd.getDisplayModes();

        for(int i=0; i < dm.length; i++) {
            if(dm[i].getWidth() == width && dm[i].getHeight() == height && dm[i].getBitDepth() == depth){
                dMode = i;
                break;
            }
        }

        // Quit if a display mode was not found.
        if( dMode == -1 ){
            runFlag = false;
            quit();
            return;
        }
        
        width2 = width / 2;
        height2 = height / 2;
        
        // Set fullscreen mode and then set the resolution.
        gd.setFullScreenWindow(win);
        gd.setDisplayMode(dm[dMode]);

        // Create a buffer strategy for drawing.
        win.createBufferStrategy(2);
        bs = win.getBufferStrategy();
        
        win.toFront();
    }
    
    public Graphics getGraphics() {
        return bs.getDrawGraphics();
    }
    
    public void show() {
        bs.show();
    }
    
    public boolean getRunFlag() {
        return runFlag;
    }
    
    public int getWidth() {
        return width;
    }
    
    public int getHeight() {
        return height;
    }
    
    public int getWidth2() {
        return width2;
    }
    
    public int getHeight2() {
        return height2;
    }
    
    private void quit() {
        gd.setFullScreenWindow(null);
        bs.dispose();
        win.dispose();
        frame.dispose();
    }
}
